package com.anolesoft.yr.mbus.cont.ctrl;

import cn.hutool.core.util.StrUtil;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.common.core.web.page.TableDataInfo;
import com.anolesoft.system.fac.entity.Employee;
import com.anolesoft.system.fac.service.IEmployeeService;
import com.anolesoft.yr.mbus.cont.entity.ContVariable;
import com.anolesoft.yr.mbus.cont.entity.ContVariableExample;
import com.anolesoft.yr.mbus.cont.service.ContVariableService;
import com.anolesoft.yr.store.stk.util.StringUtil;
import com.anolesoft.yr.util.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@AllArgsConstructor
@RestController
@RequestMapping("/cont/cont_variable")
public class ContVariableController {


    private ContVariableService contVariableService;
    private IEmployeeService iEmployeeService;
    @Transactional
    @RequestMapping(value = "/getAll",produces = {"application/json;charset=UTF-8"} )
    public AjaxResult getAll(){
        ContVariableExample contVariableExample = new ContVariableExample();
        List<ContVariable> contVariables = contVariableService.selectByExample(contVariableExample);
        return AjaxResult.success(contVariables);
    }
    @Transactional
    @RequestMapping(value = "/getById" )
    public AjaxResult getById(@RequestBody ContVariable contVariable){
        ContVariable result = contVariableService.getById(contVariable.getObjuid());
        return AjaxResult.success(result);
    }


    @Transactional
    @RequestMapping(value = "/get", produces = {"application/json;charset=UTF-8"})
    public AjaxResult get(ContVariable contVariable){
        Page<ContVariable> page = new Page<>(contVariable.getPage(), contVariable.getRows());
        String varName = StrUtil.nullToEmpty(contVariable.getVarName());
        String varType = StrUtil.nullToEmpty(contVariable.getVarType());
        LambdaQueryWrapper<ContVariable> like = new QueryWrapper<ContVariable>()
                .lambda()
                .like(ContVariable::getVarName, varName)
                .like(ContVariable::getVarType,varType)
                .notIn(ContVariable::getVarType, Lists.newArrayList("table","java"))
                .orderByDesc(ContVariable::getcTime);

        Page<ContVariable> result = contVariableService.page(page, like);
        LambdaQueryWrapper<Employee> emplike = new QueryWrapper<Employee>()
                .lambda();
        List<Employee> employees = iEmployeeService.getBaseMapper().selectList(emplike);
        Map<String, String> collect = employees.stream().collect(Collectors.toMap(Employee::getObjuid, Employee::getEmpName));


        List<ContVariable> records = result.getRecords();

        for (int i = 0; i < records.size(); i++) {
            if(records.get(i).getcUserUid()!=null){
                records.get(i).setcUserUid(collect.get(records.get(i).getcUserUid()));
            }
        }
        return AjaxResult.success(new TableDataInfo(        result.getTotal(),   records    ));
    }
    @Transactional
    @RequestMapping(value = "/del", produces = {"application/json;charset=UTF-8"})
    public AjaxResult del(ContVariable contVariable){

        contVariableService.removeById(contVariable.getObjuid());
        return AjaxResult.success("删除成功");
    }

    @Transactional
    @RequestMapping(value = "/delBatch", produces = {"application/json;charset=UTF-8"})
    public AjaxResult delBatch(@RequestBody List<ContVariable> contVariables){
        contVariableService.removeByIds(contVariables.stream().map(ContVariable::getObjuid).collect(Collectors.toList()));
        return AjaxResult.success("删除成功");
    }
    @Transactional
    @RequestMapping(value = "/edit", produces = {"application/json;charset=UTF-8"})
    public AjaxResult edit(ContVariable contVariable){
        String empUid = SecurityUtils.getLoginUser().getEmpUid();
        contVariable.setmUserUid(empUid);
        contVariable.setmTime(new Date());
        contVariableService.updateById(contVariable);
        return AjaxResult.success("修改成功");
    }
    @Transactional
    @RequestMapping(value = "/add", produces = {"application/json;charset=UTF-8"})
    public AjaxResult add(ContVariable contVariable){
        ContVariableExample contVariableExample = new ContVariableExample();
        contVariableExample.createCriteria().andVarNameEqualTo(contVariable.getVarName());
        List<ContVariable> contVariables = contVariableService.selectByExample(contVariableExample);
        if (contVariables.isEmpty()){
            String empUid = SecurityUtils.getLoginUser().getEmpUid();
            contVariable.setcUserUid(empUid);
            contVariable.setcTime(new Date());
            contVariable.setVarType("value");
            contVariable.setObjuid(StringUtil.generateRundom32String());
            contVariableService.save(contVariable);
            return AjaxResult.success("保存成功");
        }else{
            return AjaxResult.success("保存失败，变量名不可以重复");
        }
    }
    @RequestMapping(value = "/unique",  produces = {"application/json;charset=UTF-8"})
    public boolean unique(String varName,String objuid){
        objuid = StrUtil.nullToEmpty(objuid);
        ContVariableExample contVariableExample = new ContVariableExample();
        contVariableExample.createCriteria().andVarNameEqualTo(varName).andObjuidNotEqualTo(objuid);
        List<ContVariable> contVariables = contVariableService.selectByExample(contVariableExample);
        if (!contVariables.isEmpty()) {
            return false;
        }else {
            return true;
        }
    }

}
